<template>
	<div>
		<el-dialog title="导入excel" width="600px" :visible.sync="show" @close="closeForm" append-to-body>
			<el-upload v-if="!process" class="upload-demo" action :auto-upload="false" :show-file-list="false" :on-change="choose_file">
				<el-button size="mini" icon="el-icon-upload" type="primary">请选择导入excel</el-button> <span style="color:#ff0000">{{file.name}}</span>
			</el-upload>
			<el-progress v-else :percentage="percentage"></el-progress>
			<div slot="footer" class="dialog-footer">
				<el-button :size="size" :loading="loading" type="primary" @click="submit" >
					<span v-if="!loading">确 定</span>
					<span v-else>提 交 中...</span>
				</el-button>
				<el-button :size="size" @click="closeForm">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
import { importAData } from '@/api/app/saleoutstock'
	import XLSX from "xlsx"
	export default {
		name:'saleOutStockimportAData',
		props: {
			show: {
				type: Boolean,
				default: true
			},
			size: {
				type: String,
				default: 'mini'
			},
		},
		data() {
			return {
				file: "",
				process:false,
				loading:false,
				excel_import_data:[],
				percentage:0,
				page:1,
				limit:200,
			}
		},
		methods: {
			choose_file(file) {
				this.file = file
        this.page = 1 // 新增：重置页码
        this.percentage = 0 // 新增：重置进度
				this.importExcel(file)
			},
      importExcel(file) {
        var excelData = [];
        const fileReader = new FileReader();
        fileReader.onload = (ev) => {
          try {
            const data = ev.target.result;
            const workbook = XLSX.read(data, { type: "binary" });
            // 查找名为“包装作业核对表”的工作表
            const targetSheetName = workbook.SheetNames.find(name => name === "包装作业核对表");
            if (!targetSheetName) {
              throw new Error("未找到名为'包装作业核对表'的工作表");
            }
            const targetSheet = workbook.Sheets[targetSheetName]; // 获取目标 sheet 对象
            excelData = XLSX.utils.sheet_to_json(targetSheet); // 转换目标 sheet 数据为 JSON
            this.excel_import_data = excelData; // 将数据保存到组件的状态中
          } catch (e) {
            this.$message.danger('文件类型不正确');
          }
        };
        fileReader.readAsBinaryString(file.raw);
      },
			submit(){
        this.process = true
        this.loading = true
        const total_page = Math.ceil(this.excel_import_data.length/this.limit)
        // 修改进度计算逻辑：使用当前页占总页数的比例
        this.percentage = Math.ceil((this.page / total_page) * 100)

        importAData(this.getData()).then(res => {
          if(res.status == 200){
            if(this.page < total_page){
              this.page++
              this.submit() // 递归处理下一页
            }else{
              this.$message.success('导入完成')
              this.$emit('refresh_list')
              this.closeForm()
            }
          }
        }).catch(() => {
          this.loading = false
        })
      },
			getData(){
				let perdata = []
				for(let i=(this.page-1)*this.limit; i<this.page*this.limit; i++){
					if(this.excel_import_data[i]){
						perdata.push(this.excel_import_data[i])
					}
				}
				return perdata
			},
			closeForm(){
				this.$emit('update:show', false)
				this.file = ''
				this.process = false
				this.percentage = 0
				this.loading = false
				this.page = 1
				this.limit = 200
			}
		}
	}
</script>
